﻿Imports Heren.Common.Forms.Editor
Imports Heren.Common.Forms
Imports Heren.Common.ScriptEngine.Script
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports Heren.Common.Forms.Runtime
Imports System.Text
Imports Heren.Common.Libraries
Public Class 不良事件详细表 : Inherits AbstractRuntimeHandler

    ''****************************************************************
    '' 这是护理电子病历表单后台运行支持脚本
    '' 注意：这是Visual Basic脚本，您可以不区分大小写 
    '' 请不要修改系统提供的方法名，以及方法名前面的Overrides关键字
    '' Script Language : Visual Basic
    '' Author : YangMingkun, Date : 2012-3-18
    '' Copyright (C) Heren Health Services (SUPCON) Co.,Ltd.
    ''****************************************************************


    '表单脚本初始化
    Public Overrides Sub Initialize()
        'Me.ShowMessage("表单已完成加载!")
        '在此函数中来查询和设置表单中的相关数据
        '可以调用GetSystemContext函数获取系统中已有数据，也可以从数据库中读取其他任意数据

        InitializeTime()
        InitializeColumn()
        InitializeCombox()
    End Sub

    Dim dtWardInfos As DataTable = Nothing
    Dim dtPatientInfosLeft As DataTable = Nothing
    Dim array As New List(Of Column)

    Class Column
        Public type_id As String
        Public type_name As String
        Sub New(ByVal id As String, ByVal name As String)
            Me.type_id = id
            Me.type_name = name
        End Sub
    End Class

    '获取关键数据列表
    Public Overrides Function GetKeyDataList() As List(Of KeyData)
        Dim keyDataList As New List(Of KeyData)
        Return keyDataList
    End Function


    '更新表单中指定的数据
    Public Overrides Function UpdateFormData(ByVal param As String, ByVal data As Object) As Boolean
        If param = "表单数据" Then
            Me.InitializeSearch(data)
        End If
        Refresh()
        Return True
    End Function

    Public Sub InitializeSearch(ByVal data As DataTable)

    End Sub

    Public Sub InitializeTime()
        Dim xdtbegin As XDateTime = Me.FindControl("XDateTime1")
        Dim xdtend As XDateTime = Me.FindControl("XDateTime2")

        Dim MonthDays As Integer = DateTime.DaysInMonth(Today.Year, Today.Month)
        xdtbegin.value = DateSerial(Year(Today), Month(Today), 1)
        xdtend.value = DateSerial(Year(Today), Month(Today), MonthDays)
    End Sub
    '   压疮=624103
    '   跌倒坠床=962838
    '   给药错误=400886
    '   管道滑脱=629598
    '   烫伤=722168
    '   输血输液反应=867399
    '   药物外渗=320948
    '   其他=539649
    Public Sub InitializeColumn()
        array.add(New Column("624103", "压疮"))
        array.add(New Column("962838", "跌倒坠床"))
        array.add(New Column("400886", "给药错误"))
        array.add(New Column("629598", "管道滑脱"))
        array.add(New Column("722168", "烫伤"))
        array.add(New Column("867399", "输血输液反应"))
        array.add(New Column("320948", "药物外渗"))
        array.add(New Column("539649", "其他"))
    End Sub

    Public Sub InitializeCombox()
        Dim XCoboType As XComboBox = Me.FindControl("XComboBox1")
        For Each col As Column In array
            XCoboType.items.add(col.type_name)
        Next
        XCoboType.selectedindex = 0
    End Sub

    '将Array的数据返回字符串
    Public Function GetArralStr() As String
        Dim strarray As String = String.empty
        For Each col As Column In array
            strarray += col.type_id & ","
        Next
        Return strarray.substring(0, strarray.length - 1)
    End Function

    '将Array的数据返回字符串
    Public Function GetIDFromName(ByVal strname As String) As String
        For Each col As Column In array
            If col.type_name = strname Then
                Return col.type_id
            End If
        Next
        Return ""
    End Function

    Private Sub Refresh()
        BindLeftGrid(dtWardInfos)
        ViewLeft()
    End Sub

    'select (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = 'ReportType') as "事件类别",
    '       (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人科室') as "病人科室",
    '       (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人床号') as "病人床号",
    '       (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人住院号') as "病人住院号",
    '       (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人姓名') as "病人姓名",
    '       (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人诊断') as "病人诊断",
    '       (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '是否发生医疗事故') as "是否发生医疗事故",
    '       t.doc_id,t.data_time,t.patient_id,t.ward_code,t.doctype_id
    '  from doc_summary_data t
    '  where t.doctype_id in (624103,962838,400886,629598,722168,867399,320948,539649)
    '  and t.ward_code in ('HL0501')
    '  group by t.doc_id,t.data_time,t.patient_id,t.ward_code,t.doctype_id
    '  order by data_time desc

    '获取左表数据列表
    Private Sub BindLeftGrid(ByVal dtWard As DataTable)
        Dim xdtbegin As XDateTime = Me.FindControl("XDateTime1")
        Dim xdtend As XDateTime = Me.FindControl("XDateTime2")
        Dim XCoboType As XComboBox = Me.FindControl("XComboBox1")
        Dim sql As New StringBuilder()
        sql.Append("select (select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = 'ReportType') as ""事件类别"",")
        sql.Append("(select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人科室') as ""病人科室"",")
        sql.Append("(select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人床号') as ""病人床号"",")
        sql.Append("(select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人住院号') as ""病人住院号"",")
        sql.Append("(select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人姓名') as ""病人姓名"",")
        sql.Append("(select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '病人诊断') as ""病人诊断"",")
        sql.Append("(select n.data_value from doc_summary_data n where t.doc_id = n.doc_id and n.data_name = '是否发生医疗事故') as ""是否发生医疗事故"",")
        sql.Append("t.doc_id,t.data_time,t.patient_id,t.ward_code,t.doctype_id ")
        sql.Append("from doc_summary_data t ")
        sql.Append("where 1=1 ")
        If XCoboType.selectedindex = 0 Then
            sql.AppendFormat("and t.doctype_id in ({0}) ", GetArralStr())
        Else
            sql.appendFormat("and t.doctype_id = {0} ", GetIDFromName(XCoboType.Text))
        End If
        If dtWardInfos IsNot Nothing Then
            sql.AppendFormat("and t.WARD_CODE in ({0}) ", GetStrWards(dtWard))
        End If
        sql.AppendFormat("and t.data_time - to_date('{0}','yyyy-mm-dd hh24:mi:ss')>=0 ", xdtbegin.value)
        sql.AppendFormat("and to_date('{0}','yyyy-mm-dd hh24:mi:ss') - t.data_time > -1 ", xdtend.value)  '大于 -1是因为在计算时间差时时分秒的计算误差
        sql.Append("group by t.doc_id,t.data_time,t.patient_id,t.ward_code,t.doctype_id ")
        sql.Append("order by data_time desc ")
        showmessage(sql.tostring)
        Dim dataSet As New DataSet
        Me.ExecuteQuery(sql.ToString(), dataSet)
        dtPatientInfosLeft = dataSet.Tables(0)

    End Sub

    Private Function GetStrWards(ByVal dtWardInfos As DataTable) As String
        Dim strwards As String = String.Empty
        For Each dr As DataRow In dtWardInfos.Rows
            strwards += "'" & dr(0) & "',"
        Next
        'showmessage(strwards.length)
        strwards = strwards.substring(0, strwards.length - 1)
        Return strwards
    End Function

    '获取表单中指定的数据
    Public Overrides Function GetFormData(ByVal param As Object) As Object
        If param = "表单摘要" Then
            Return Me.GetFormSummary()
        End If
        Return Nothing
    End Function

    '获取表单摘要数据
    Public Function GetFormSummary() As String
        Dim control As Control = Me.FindControl("XTextBox1")
        If control IsNot Nothing Then
            Return control.Text
        End If
        Return String.Empty
    End Function


    '双击组件时发生。
    Private Sub XTextBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '调用科室选择窗口
        Dim control As Control = Me.FindControl("XTextBox1")
        '当前选中的护理单元
        Dim dtCurrentWardInfos As DataTable = Nothing
        If control IsNot Nothing Then
            dtCurrentWardInfos = control.Tag
        End If
        '传出选择的护理单元
        Dim strDeptNames As String = ""
        Me.RaiseCustomEvent(sender, "病区选择对话框", "true", dtCurrentWardInfos, dtCurrentWardInfos)
        If dtCurrentWardInfos IsNot Nothing Then
            For Each dr As DataRow In dtCurrentWardInfos.Rows
                If strDeptNames = "" Then
                    strDeptNames = Me.GetFieldValue(dr, 1)
                Else
                    strDeptNames += ";" + Me.GetFieldValue(dr, 1)
                End If
            Next
        End If
        If strDeptNames <> "" Then
            Me.SetControlText("XTextBox1", strDeptNames)
        Else
            Me.SetControlText("XTextBox1", "全院<可双击修改>")
        End If
        If control IsNot Nothing Then
            control.Tag = dtCurrentWardInfos
        End If
        dtWardInfos = dtCurrentWardInfos
    End Sub

    '单击组件时发生。
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Refresh()
    End Sub

    '双击 DataGridView 中的单元格时发生。
    Private Sub XDataGrid1_CellMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
        'e.ColumnIndex, System.Int32
        'e.RowIndex, System.Int32
        'e.Button, System.Windows.Forms.MouseButtons, 可取值为:
        'If e.Button = System.Windows.Forms.MouseButtons.None Then
        'If e.Button = System.Windows.Forms.MouseButtons.Left Then
        'If e.Button = System.Windows.Forms.MouseButtons.Right Then
        'If e.Button = System.Windows.Forms.MouseButtons.Middle Then
        'If e.Button = System.Windows.Forms.MouseButtons.XButton1 Then
        'If e.Button = System.Windows.Forms.MouseButtons.XButton2 Then

        'e.Clicks, System.Int32
        'e.X, System.Int32
        'e.Y, System.Int32
        'e.Delta, System.Int32
        'e.Location, System.Drawing.Point

    End Sub


    '将查询数据显示在左列表上
    Public Sub ViewLeft()
        Dim xgdvleft As XDataGrid = Me.FindControl("XDataGrid1")
        xgdvleft.rows.clear()
        Dim RowNo As Integer = 0
        Dim Counts As Integer = 0
        For Each dr As DataRow In dtPatientInfosLeft.Rows
            xgdvleft.Rows.Add()
            xgdvleft.Rows(RowNo).Cells(0).value = dr("事件类别")
            xgdvleft.Rows(RowNo).Cells(1).value = dr("病人科室")
            xgdvleft.Rows(RowNo).Cells(2).value = dr("病人床号")
            xgdvleft.Rows(RowNo).Cells(3).value = dr("病人住院号")
            xgdvleft.Rows(RowNo).Cells(4).value = dr("病人姓名")
            xgdvleft.Rows(RowNo).Cells(5).value = dr("病人诊断")
            xgdvleft.Rows(RowNo).Cells(6).value = dr("是否发生医疗事故")
            RowNo = RowNo + 1
        Next
    End Sub



End Class
